package com.llbnk.springcoding.thread.rejectedPolicy;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ThreadPoolExecutor;


@Slf4j
public class DiscardPolicyBuildHandler extends ThreadPoolExecutor.CallerRunsPolicy {

    private final String poolName;


    public DiscardPolicyBuildHandler(String poolName) {
        this.poolName = poolName;
    }

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        String msg = String.format(
                "Thread pool is EXHAUSTED!"
                        + " Pool Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: %d),"
                        + " Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s)!",
                poolName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(),
                e.getLargestPoolSize(), e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(),
                e.isTerminating());
        //add log and monitor
        log.warn("discard policy " + msg);
        super.rejectedExecution(r, e);
    }
}
